package com.valdemar.cookerybook.resources;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.UriInfo;

import com.valdemar.cookerybook.dao.IngredientDao;
import com.valdemar.cookerybook.model.Ingredient;

@Path("/receipt/ingredient")
public class IngredientsResource {
	@Context
	UriInfo uriInfo;
	@Context
	Request request;
	String id;

	// public IngredientResource(UriInfo uriInfo, Request request, String id) {
	// this.uriInfo = uriInfo;
	// this.request = request;
	// this.id = id;
	// }

	// retuns the number of todos
	// Use http://localhost:8080/de.vogella.jersey.todo/rest/todos/count
	// to get the total number of records
	@GET
	@Path("count")
	@Produces(MediaType.APPLICATION_JSON)
	public String getCount() {
		int count = IngredientDao.instance.getCount();
		return String.valueOf(count);
	}

	// Return the list of todos for applications
	@GET
	@Produces({ MediaType.APPLICATION_JSON })
	public List<Ingredient> getIngredients( @PathParam("languageId") int languageId) {
		List<Ingredient> ingredients = new ArrayList<Ingredient>();
		ingredients.addAll(IngredientDao.instance.listAll(languageId));
		return ingredients;
	}

//	@POST
//	@Produces(MediaType.APPLICATION_JSON)
//	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
//	public void newIngredient(
//			@FormParam("code") String code,
//			@FormParam("languageID") int langID,
//			@FormParam("translation") String translation,
//			@Context HttpServletResponse servletResponse) throws IOException {
//		IngredientDAO.instance.add(code, langID, translation);
//	}
//	
	
	@POST
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public void newIngredient(
			@FormParam("ingredients") String ingredients, 
			@FormParam("languageId") int languageId, 
			@Context HttpServletResponse servletResponse) throws IOException
	{
		IngredientDao.instance.addBulk(ingredients, languageId);
	}
	
	

	// @GET
	// @Produces({MediaType.APPLICATION_JSON})
	// public Ingredient getTodo() {
	// Ingredient todo = IngredientDAO.instance.getModel().get(id);
	// if(todo==null)
	// throw new RuntimeException("Get: Todo with " + id + " not found");
	// return todo;
	// }
	
	// Defines that the next path parameter after todos is
	// treated as a parameter and passed to the TodoResources
	// Allows to type http://localhost:8080/de.vogella.jersey.todo/rest/todos/1
	// 1 will be treaded as parameter todo and passed to TodoResource
	@Path("{ingredient_code}/{languageId}")
	public IngredientResource getIngredient(
			@PathParam("ingredient_code") String code,
			@PathParam("languageId") int languageId) 
	{
		return new IngredientResource(uriInfo, request, code, languageId);
	}

}
